package com.zx.eval.service.impl;

import com.zx.FastApplication;
import com.zx.eval.entity.MemberEntity;
import com.zx.eval.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.common.utils.PageUtils;
import com.zx.common.utils.Query;

import com.zx.eval.dao.EvalTableDao;
import com.zx.eval.entity.EvalTableEntity;
import com.zx.eval.service.EvalTableService;


@Service("evalTableService")
public class EvalTableServiceImpl extends ServiceImpl<EvalTableDao, EvalTableEntity> implements EvalTableService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<EvalTableEntity> page = this.page(
                new Query<EvalTableEntity>().getPage(params),
                new QueryWrapper<EvalTableEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public EvalTableEntity getCorrespondingTable(Integer id, Long memberId) throws ParseException {
        EvalTableEntity evalTable = this.getById(id);
        String group = evalTable.getGroupName();
        EvalTableEntity result;
        //如果没有年龄限制则返回当前测评
        if(evalTable.getMinMonth() ==null || evalTable.getMaxMonth() == null){
            result = evalTable;
        }else {
            MemberEntity member = memberService.getById(memberId);
            String birthday = member.getBirthday();
            int i = countMonths(birthday, new Date());
            QueryWrapper<EvalTableEntity> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.le("min_month", i);
            objectQueryWrapper.ge("max_month", i);
            objectQueryWrapper.eq("display", 1);
            objectQueryWrapper.eq("group_name", group);
            result = this.getOne(objectQueryWrapper);
        }

        return result;
    }

    private int countMonths(String startDate, Date endDate) throws ParseException {
        int monthday;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate1 = fmt.parse(startDate);

        Calendar starCal = Calendar.getInstance();
        starCal.setTime(startDate1);

        int sYear = starCal.get(Calendar.YEAR);
        int sMonth = starCal.get(Calendar.MONTH);
        int sDay = starCal.get(Calendar.DAY_OF_MONTH);

        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDate);
        int eYear = endCal.get(Calendar.YEAR);
        int eMonth = endCal.get(Calendar.MONTH);
        int eDay = endCal.get(Calendar.DAY_OF_MONTH);

        monthday = ((eYear - sYear) * 12 + (eMonth - sMonth));

        //这里计算零头的情况，根据实际确定是否要加1 还是要减1
        if (sDay < eDay) {
            monthday = monthday + 1;
        }
        return monthday;
    }

    @Autowired
    private MemberService memberService;
}
